<?php
namespace Emos\Editor;

class Path {      
    protected $nodes = array();

    public function __construct() {
    }
    
    public function addNode(Node $node) {
        $this->nodes[] = $node;
    }    
    
    public function getNodes() {
        return $this->nodes;
    }    

    public function getLength() {
        return count($this->nodes);
    }     
    
    public function getNode($num) {
        if(($num >= 0) && ($num < count($this->nodes))) {
            return $this->nodes[$num];
        }
        else {
            return null;
        }        
    }    

    public function getNodeFromEnd($num) {
        if(($num >= 0) && ($num < count($this->nodes))) {
            return $this->nodes[count($this->nodes)-1 - $num];
        }
        else {
            return null;
        }        
    }        
    
    public function isSubpath(Path $subpath) {
        foreach($subpath as $s => $subpathNode) {
            $node = @$this->nodes[$s];
            if($node === null) {
                return false;
            }
            else {
                if(!$subpathNode->equal($node)) {
                    return false;
                }
            }
        }
        
        return true;        
    }    
    
    public function __toString() {
        $str = "";
        foreach($this->nodes as $node) {
            $str .= " - ".$node;
        }
        return $str;
    }
}